package com.handong.handserialprogram.module.tasklist;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.model.entities.MissionEntity;
import com.handong.handserialprogram.module.choosering.ChooseRingActivity;
import com.handong.handserialprogram.module.singletask.MissionActivity;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.SPUtil;

import java.util.List;

/**
 * Created by Administrator on 2016/12/10.
 */

public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.ViewHolder> {

    private List<MissionEntity> mTaskList;
    private TaskListActivity mActivity;

    public TaskListAdapter(TaskListActivity activity, List<MissionEntity> listEntities) {
        this.mActivity = activity;
        mTaskList = listEntities;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_task_item, parent, false);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MissionEntity entity = mTaskList.get(position);
        holder.mEntity = entity;
        holder.taskName.setText(entity.getTaskName());
        holder.taskBuildTime.setText(entity.getTaskStartDate());
        if (entity.isTaskHasCriminal()) {
            holder.taskCar.setText(R.string.mission_has_criminal);
        } else {
            holder.taskCar.setText(R.string.mission_no_criminal);
        }

        GradientDrawable drawable = (GradientDrawable) holder.taskStatus.getBackground();
        int taskType = entity.getTaskType();
        if (0 == taskType) {
            drawable.setColor(ColorContant.MISSION_NORMAL);
            holder.taskStatus.setText(R.string.mission_type_normal);
        } else {
            drawable.setColor(ColorContant.MISSION_SIMPLE);
            holder.taskStatus.setText(R.string.mission_type_simple);
        }
        if (entity.ispInTask()) {
            holder.taskDevice.setText(R.string.mission_device_own);
        } else {
            if (entity.isCanJoin()){
                holder.taskDevice.setText("暂无");
            }
            else{
                holder.taskDevice.setText(R.string.mission_device_other);
            }

        }
    }

    public void setTaskList(List<MissionEntity> taskList) {
        mTaskList = taskList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mTaskList == null ? 0 : mTaskList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private MissionEntity mEntity;

        private TextView taskName, taskBuildTime, taskCar, taskStatus, taskDevice;

        public ViewHolder(View itemView) {
            super(itemView);
            taskName = (TextView) itemView.findViewById(R.id.taskItemName);
            taskBuildTime = (TextView) itemView.findViewById(R.id.taskItemBuildTime);
            taskCar = (TextView) itemView.findViewById(R.id.taskItemTaskCar);
            taskStatus = (TextView) itemView.findViewById(R.id.taskItemStatus);
            taskDevice = (TextView) itemView.findViewById(R.id.taskItemTaskDevice);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putString(KeyConstant.MISSION_ID, mEntity.getTaskId());
            bundle.putString(KeyConstant.MISSION_NAME, mEntity.getTaskName());
            bundle.putString(KeyConstant.MISSION_TIME, mEntity.getTaskStartDate());
            bundle.putSerializable(KeyConstant.CRIMINALS_LIST,mEntity.getCriminal());
            bundle.putBoolean(KeyConstant.IS_REJOIN,true);
            bundle.putString(KeyConstant.MISSION_CAR, mEntity.getTaskEscortCar());
            if (mEntity.ispInTask()) {
                if (mEntity.isTaskHasCriminal()) {

                    mActivity.openActivity(MissionActivity.class, bundle);
                } else {
                    bundle.putBoolean(KeyConstant.IS_JOIN, false);
                    mActivity.openActivity(MissionActivity.class, bundle);
                }
            } else {

                if (mActivity.isMissionExecuting()) {//检查未完成的任务
                    return;
                }
                if (mEntity.isCanJoin()) {
                    bundle.putBoolean(KeyConstant.IS_JOIN, true);
                    mActivity.showJoinTipDialog(bundle);
                }
                else{
                    AppUtil.showLengthToast(mActivity,"当前任务正在由其它设备执行,不可加入！");
                }
            }
        }


    }
}
